import { request, get } from '../ajax'
import { getAjaxBaseUrl } from '../handler'
import type { STORAGE_MODES } from '@/enum/modules/indicator'

const controller = getAjaxBaseUrl('/template', '/energy')

/** 根据模型编码查询模型树 */
export function getTemplateTree(modelCode: string) {
  return get<any>(`${controller}/${modelCode}`)
}

/** 导出 */
export function exportData(params: any) {
  return request({
    url: `${controller}/export`,
    method: 'post',
    data: params,
    responseType: 'blob',
  })
}

/** template_001 */
export function exportData1(params: any) {
  return post(`${controller}/monio-export`, params)
}

/** template_002 */
export function exportData2(params: any) {
  const data = {
    ...params,
    cycle: ({
      '1H': 1,
      '1d': 2,
      '1M': 3,
    } as any)[params.timeType],
  }
  delete data.timeType
  return post(`${controller}/monio-export2`, data)
}

/** 分页查询表格列表数据(template_001) */
export function getTableList(params: any) {
  return get(`${controller}/page`, params)
}

/** 分页查询表格列表数据(template_002) */
export function getTableList2(params: any) {
  const data = {
    ...params,
    cycle: ({
      '1H': 1,
      '1d': 2,
      '1M': 3,
    } as any)[params.timeType],
  }
  delete data.timeType
  return get(`${controller}/page2`, data)
}

export interface Template003ParamsType {
  modelCode: string
  timeType: STORAGE_MODES
  dataTime: string
  tagId: string | null
}

export interface GetTopTagTotalResultItem {
  tagId: string | null
  name: string
  unit: string
  indicatorData: {
    val: number
    yoy: number
    yoyVal: number
    mom: number
    momVal: number
  }
}

export interface GetTopTagDetailsResult {
  rowData: string[]
  tagDataList: GetTopTagTotalResultItem[]
}

export interface QuerySangitaChartsResult {
  nodes: string[]
  links: { source: string, target: string, value: number }[]
}

/** template_003 卡片数据 */
export function getTopTagTotal(params: Template003ParamsType) {
  return post<GetTopTagTotalResultItem[]>(`${controller}/getTopTagTotal`, params)
}

/** template_003 折线图数据 */
export function getTopTagDetails(params: Template003ParamsType) {
  return post<GetTopTagDetailsResult>(`${controller}/getTopTagDetails`, params)
}

/** template_003 桑吉图数据 */
export function querySangitaCharts(params: Template003ParamsType) {
  return post<QuerySangitaChartsResult>(`${controller}/querySangitaCharts`, params)
}

export interface ModelTreeItem {
  id: number
  code: string
  name: string
  children: ModelTreeItem[]
}

export interface LastNodeItem {
  id: number
  name: string
}

export interface LastNodeDataItem {
  name: string
  unit: string
  rowData: string[]
  tagDataVoList: { name: string, id: number, nowData: Record<string, number> }[]
}

/** template_005 根据modelCode获取模型树 */
export function getModelTree(modelCode: string) {
  return get<ModelTreeItem>(`${controller}/${modelCode}`)
}

/** template_005 根据modelId获取最后一级的所有模型节点 */
export function loadAllLastModelTag(modelId?: number, modelCode?: string) {
  return get<LastNodeItem[]>(`${controller}/loadAllLastModelTag`, { modelId, modelCode })
}

/** template_005 根据modelId获取节点数据 */
export function loadAllLastModelData(params: {
  modelId?: number
  modelCode?: string
  timeType: STORAGE_MODES
  dataTime: string
}) {
  return post<LastNodeDataItem>(`${controller}/loadAllLastModelData`, params)
}
